<template>
  <div id="basicLayout">
    <a-layout style="min-height: 100vh">
      <a-layout-header class="header">
        <GlobalHeader />
      </a-layout-header>
      <a-layout>
        <a-layout-sider class="sider" width="220">
          <a-menu
            v-model:selectedKeys="selectedKeys"
            theme="light"
            mode="inline"
            :items="menuItems"
            @click="handleMenuClick"
          />
        </a-layout-sider>
        <a-layout>
          <a-layout-content class="content">
            <router-view />
          </a-layout-content>
          <a-layout-footer class="footer">
            <a href="https://www.baidu.com" target="_blank">云图库 All Rights Reserved</a>
          </a-layout-footer>
        </a-layout>
      </a-layout>
    </a-layout>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import GlobalHeader from '@/components/Header/GlobalHeader.vue'
import {
  HomeOutlined,
  UserOutlined,
  AppstoreOutlined,
  SettingOutlined,
} from '@ant-design/icons-vue'
import { h } from 'vue'
import type { MenuProps } from 'ant-design-vue/es/menu/src/Menu'

const router = useRouter()
const route = useRoute()
const selectedKeys = ref<string[]>([route.path])

// 添加图标数组
const iconArray = [HomeOutlined, UserOutlined, AppstoreOutlined, SettingOutlined]

const menuItems = ref<MenuProps['items']>(
  router
    .getRoutes()
    .filter((route) => route.meta.showInNav)
    .map((route) => {
      const randomIcon = iconArray[Math.floor(Math.random() * iconArray.length)]
      return {
        key: route.path,
        icon: () => h(randomIcon), // 随机选择一个图标
        label: String(route.name), // 确保 label 是字符串
        title: String(route.name), // 确保 title 是字符串
      }
    }),
)

// 处理菜单点击
const handleMenuClick = ({ key }: { key: string }) => {
  router.push(key)
}

// 监听路由变化，更新选中的菜单项
watch(
  () => route.path,
  (newPath) => {
    selectedKeys.value = [newPath]
  },
)
</script>

<style scoped>
#basicLayout .header {
  padding: 0;
  background: linear-gradient(160deg, #eaf2fa, #d8eaf8, #c5e7f8, #bce7fa);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1;
  margin: 8px 8px 0 0;
  border-radius: 8px;
  height: 56px;
  line-height: 56px;
}

#basicLayout .main-container {
  flex: 1;
  background: #f8fafc;
}

#basicLayout .sider {
  background: linear-gradient(180deg, #0ea5e9, #0284c7);
  margin: 8px 0 0 0;
  border-radius: 0 8px 0 0;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  height: calc(100vh - 64px);
  position: fixed;
  left: 0;
  overflow: auto;
}

#basicLayout .content {
  margin-left: 236px;
  padding: 24px;
  min-height: calc(100vh - 104px - 70px);
}

#basicLayout .footer {
  margin-left: 236px;
  text-align: center;
  background: transparent;
  padding: 16px;
  color: rgba(0, 0, 0, 0.45);
}

:deep(.ant-layout) {
  background: #f8fafc;
}

:deep(.ant-layout-sider-children) {
  display: flex;
  flex-direction: column;
}

:deep(.ant-menu) {
  flex: 1;
  border-right: none;
  background: transparent !important;
}

:deep(.ant-menu-item) {
  margin: 4px 8px !important;
  border-radius: 6px;
  color: rgba(255, 255, 255, 0.85) !important;
  border: 1px solid transparent;
  transition: all 0.3s ease;
}

:deep(.ant-menu-item:hover) {
  background-color: rgba(255, 255, 255, 0.1) !important;
  color: white !important;
  border-color: rgba(255, 255, 255, 0.3);
}

:deep(.ant-menu-item.ant-menu-item-selected) {
  background: rgba(255, 255, 255, 0.2) !important;
  color: white !important;
  border: 2px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.1);
}

:deep(.ant-menu-item .anticon) {
  color: rgba(255, 255, 255, 0.85) !important;
}

:deep(.ant-menu-item:hover .anticon),
:deep(.ant-menu-item.ant-menu-item-selected .anticon) {
  color: white !important;
}

:deep(.ant-menu-inline .ant-menu-item::after) {
  display: none;
}

/* 自定义滚动条样式 */
.sider::-webkit-scrollbar {
  width: 6px;
}

.sider::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.3);
  border-radius: 3px;
}

.sider::-webkit-scrollbar-track {
  background: transparent;
}

.sider::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.5);
}
</style>
